package tk.mybatis.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.springboot.mapper.RegionMapper;
import tk.mybatis.springboot.mapper.SitesMapper;
import tk.mybatis.springboot.model.Region;
import tk.mybatis.springboot.model.Sites;

import java.util.Date;
import java.util.UUID;

/**
 * Created by  on 2017/4/12.
 */
@Controller
@RequestMapping("/region")
@Transactional
public class  RegionController {

    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private SitesMapper sitesMapper;

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Region obj){
        if(StringUtils.isEmpty(obj.getId())) {
            obj.setId(UUID.randomUUID().toString());
            return regionMapper.insertSelective(obj);
        }
        return regionMapper.updateByPrimaryKeySelective(obj);
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable String id){
        return regionMapper.deleteByPrimaryKey(id);
    }

    @RequestMapping("/addSite")
    public Object addSite(String siteId,String regionId){
        sitesMapper.updateByPrimaryKeySelective(new Sites(){
            {
                setId(siteId);
                setRegionId(regionId);
            }
        });
        return null;
    }
    @RequestMapping("/deleteSite")
    public Object deleteSite(String siteId,String regionId){
        sitesMapper.updateByPrimaryKeySelective(new Sites(){
            {
                setId(siteId);
                setRegionId("");
            }
        });
        return null;
    }
}
